package com.ticket.sass.provider.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.common.entity.tenant.FilmVersionEntity;
import com.ticket.sass.provider.dto.film.FilmVersionOutput;
import com.ticket.sass.provider.mapper.FilmVersionMapper;
import com.ticket.sass.provider.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-07
 */
@Service
public class FilmVersionServiceImpl extends ServiceImpl<FilmVersionMapper, FilmVersionEntity> implements BaseService<FilmVersionEntity> {
    /**
     * 根据 code 查询对应的 filmId
     * @param codes code 列表
     * @return Map，键为 code，值为对应的 filmId
     */
    public Map<String, Long> getFilmIdsByCodes(List<String> codes) {
        if (codes.size() == 0){
            return new HashMap<>();
        }
        
        
        // 构建查询条件，根据 code 列表查询
        QueryWrapper<FilmVersionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code", codes);
        
        // 调用 MyBatis-Plus 的方法查询数据库
        List<FilmVersionEntity> entities = baseMapper.selectList(queryWrapper);
        
        // 构建 Map 以 code 为键，filmId 为值
        Map<String, Long> resultMap = new HashMap<>();
        for (FilmVersionEntity entity : entities) {
            resultMap.put(entity.getCode(), entity.getFilmId());
        }
        
        return resultMap;
    }
    
    public boolean areCodeListNotExist(List<String> codeList) {
        if (codeList.size() == 0){
            return false;
        }
        
        // 构造查询条件
        QueryWrapper<FilmVersionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code", codeList);
        
        // 查询数据库并判断结果数量
        return baseMapper.selectCount(queryWrapper) == 0;
    }
    
    /**
     * 根据电影ID获取发行版本列表
     *
     * @param movieId 电影ID
     * @return 电影发行版本列表
     */
    public List<FilmVersionEntity> getByMovieId(Long movieId) {
        // 构造查询条件
        QueryWrapper<FilmVersionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("film_id", movieId);
        
        // 查询数据库并返回结果
        return baseMapper.selectList(queryWrapper);
    }
    

    
    public List<Integer> findMovieIdsByCode(String code) {
        if (StrUtil.isBlankIfStr(code)){
            return new ArrayList<>();
        }
        
        QueryWrapper<FilmVersionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct film_id").like("code", code);
        
        return listObjs(queryWrapper, obj -> Integer.valueOf(obj.toString()));
    }
    
    /**
     * 根据影片ID列表获取对应的发行版本列表
     *
     * @param filmIds 影片ID列表
     * @return Map，键为影片ID，值为对应的发行版本列表
     */
    public Map<Long, List<FilmVersionOutput>> getFilmVersionsByFilmIds(List<Long> filmIds) {
        if (filmIds.size() == 0){
            return new HashMap<>();
        }
     
        // 根据影片ID列表查询对应的发行版本列表
        List<FilmVersionEntity> filmVersionEntities = baseMapper.selectList(new QueryWrapper<FilmVersionEntity>().in("film_id", filmIds));
        
        // 将查询结果按影片ID分组，并转换为Map
        Map<Long, List<FilmVersionOutput>> filmVersionsMap = new HashMap<>();
        for (Long filmId : filmIds) {
            List<FilmVersionOutput> versions = filmVersionEntities.stream()
                .filter(entity -> entity.getFilmId().equals(filmId))
                .map(this::convertToOutput)
                .collect(Collectors.toList());
            filmVersionsMap.put(filmId, versions);
        }
        
        return filmVersionsMap;
    }
    
    /**
     * 将FilmVersionEntity对象转换为FilmVersionOutput对象
     *
     * @param entity FilmVersionEntity对象
     * @return FilmVersionOutput对象
     */
    private FilmVersionOutput convertToOutput(FilmVersionEntity entity) {
        FilmVersionOutput output = new FilmVersionOutput();
        output.setName(entity.getName());
        output.setCode(entity.getCode());
        return output;
    }
}
